package com.lsh.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lsh.common.util.PageSurport;
import com.lsh.dao.TsysUserDao;
import com.lsh.dao.mapper.TsysUserMapper;
import com.lsh.model.TsysMenu;
import com.lsh.model.TsysUser;

/**
 * @author lsh
 *
 */
@Service("tsysUserService")
public class TsysUserService extends BasicService{

	private final static Log LOG = LogFactory.getLog(TsysUser.class);
	@Autowired
	TsysUserDao dao;
	@Autowired
	TsysUserMapper mapper;
	@Autowired
	TsysMenuService menuService;
	/**
	 * 插入数据
	 * @param tsysUser
	 * @return
	 */
	public int insert(TsysUser tsysUser) {
		return dao.insert("TsysUser-insert", tsysUser);
	}

	/**
	 * 根据ID获取记录
	 * @param tsysUser
	 * @return
	 */
	public TsysUser getById(String no) {
		return dao.getById("TsysUser-getById", no);
	}

	/**
	 * 删除记录
	 * @param object
	 * @return
	 */
	public int delete(TsysUser tsysUser) {
		return dao.delete("TsysUser-delete", tsysUser);
	}

	/**
	 * 获取分页数据
	 * @param map
	 * @param page
	 * @return
	 */
	public PageSurport getPage(Map map, PageSurport page) {
		return dao.selectPageByPage("TsysUser.getPage",
				"TsysUser-getPageCount", page, map);
	}

	/**获取全部记录
	 * @param orderBy
	 * @return
	 */
	public List getAll(String orderBy) {
		return dao.selectAll("TsysUser-getAll", orderBy);
	}

	
	/**		更新记录
	 * @param tsysUser
	 */
	public void update(TsysUser tsysUser) {
		dao.update("TsysUser-update", tsysUser);
	}

	public List getMenuListByUser(TsysUser user) {
		String parentmenuid = "";
		List<TsysMenu> list = mapper.getMenuListByUser(user);
		List<TsysMenu> list2 = new ArrayList<TsysMenu>();
		TsysMenu t1_menu = new TsysMenu();
		for (TsysMenu menu : list) {
			TsysMenu parent = menuService.getById(menu.getParentid());
			menu.setParentMenu(parent);
			if (!(parentmenuid.equals(menu.getParentMenu().getMenuid()))
					|| "".equals(parentmenuid)) {
				t1_menu = parent;
				t1_menu.setMenuid(parent.getMenuid());
				t1_menu.setParentid(parent.getParentid());
				t1_menu.setMenuname(parent.getMenuname());
				list2.add(t1_menu);
			}
			t1_menu.getChildMenus().add(menu);
			parentmenuid = menu.getParentMenu().getMenuid();
		}
		return list2;
	}

	/**
	 * 功能：取用户权限列表
	 * @param user
	 * @return HashSet
	 */
	public HashSet<String> getRightSetByUser(TsysUser user) 
	{
//		String sql = "select upper(rightcode) from v_userright Where userid=" + user.getUserid();
//		sql = "select  upper(rightcode) from tsys_right where righttype='SYS' and nodetype=1 ";
		List<String> list=null;
		if(user.getUsertype()==0)
		{//超级管理员取所有权限
			list = mapper.getRightSetByManager(user);
		}else{
			
			list = mapper.getRightSetByUser(user);
		}
		HashSet<String> set = new HashSet<String>(list);
		return set;
	}
}
